package com.ruoyi.payment.service.impl;

import com.ruoyi.payment.mapper.RoomDataMapper;
import com.ruoyi.payment.service.RoomDataService;
import com.ruoyi.pojo.TbBuildingblock;
import com.ruoyi.pojo.TbFloor;
import com.ruoyi.pojo.TbRoom;
import com.ruoyi.pojo.TbUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现 RoomDataService 接口的服务实现类，用于提供房间数据的业务逻辑。
 */
@Service
public class RoomDataServiceImpl implements RoomDataService {

    @Autowired
    private RoomDataMapper roomDataMapper;

    /**
     * 获取完整的房间数据结构。
     *
     * @return 房间数据列表，每个元素是一个 Map 对象，包含楼栋、单元、楼层和房间信息。
     */
    @Override
    public List<Map<String, Object>> fetchRoomData() {
        // 1. 获取所有楼栋信息
        List<TbBuildingblock> buildingblocks = roomDataMapper.selectAllBuildingblocks();

        // 2. 遍历楼栋，获取每个楼栋下的单元、楼层和房间信息
        List<Map<String, Object>> allOptions = new ArrayList<>();
        for (TbBuildingblock buildingblock : buildingblocks) {
            // 创建楼栋选项
            Map<String, Object> buildingblockOption = new HashMap<>();
            buildingblockOption.put("value", buildingblock.getId());
            buildingblockOption.put("label", buildingblock.getBlockNumber());

            List<Map<String, Object>> unitOptions = new ArrayList<>();
            // 获取该楼栋下的所有单元信息
            List<TbUnit> units = getUnitsByBuildingblockId(buildingblock.getId());
            for (TbUnit unit : units) {
                // 创建单元选项
                Map<String, Object> unitOption = new HashMap<>();
                unitOption.put("value", unit.getId());
                unitOption.put("label", unit.getUnitNumber());

                List<Map<String, Object>> floorOptions = new ArrayList<>();
                // 获取该单元下的所有楼层信息
                List<TbFloor> floors = getFloorsByUnitId(unit.getId());
                for (TbFloor floor : floors) {
                    // 创建楼层选项
                    Map<String, Object> floorOption = new HashMap<>();
                    floorOption.put("value", floor.getId());
                    floorOption.put("label", floor.getFloorNumber());

                    List<Map<String, Object>> roomOptions = new ArrayList<>();
                    // 获取该楼层下的所有房间信息
                    List<TbRoom> rooms = getRoomsByFloorId(floor.getId());
                    for (TbRoom room : rooms) {
                        // 创建房间选项
                        Map<String, Object> roomOption = new HashMap<>();
                        roomOption.put("value", room.getId());
                        roomOption.put("label", room.getRoomNumber());

                        roomOptions.add(roomOption);
                    }

                    // 将房间选项添加到楼层选项中
                    floorOption.put("children", roomOptions);
                    floorOptions.add(floorOption);
                }

                // 将楼层选项添加到单元选项中
                unitOption.put("children", floorOptions);
                unitOptions.add(unitOption);
            }

            // 将单元选项添加到楼栋选项中
            buildingblockOption.put("children", unitOptions);
            allOptions.add(buildingblockOption);
        }

        // 返回完整的房间数据结构
        return allOptions;
    }

    /**
     * 根据楼栋 ID 获取该楼栋下的所有单元信息。
     *
     * @param buildingblockId 楼栋 ID
     * @return 单元列表
     */
    private List<TbUnit> getUnitsByBuildingblockId(Integer buildingblockId) {
        return roomDataMapper.selectUnitsByBuildingblockId(Long.valueOf(buildingblockId));
    }

    /**
     * 根据单元 ID 获取该单元下的所有楼层信息。
     *
     * @param unitId 单元 ID
     * @return 楼层列表
     */
    private List<TbFloor> getFloorsByUnitId(Integer unitId) {
        return roomDataMapper.selectFloorsByUnitId(Long.valueOf(unitId));
    }

    /**
     * 根据楼层 ID 获取该楼层下的所有房间信息。
     *
     * @param floorId 楼层 ID
     * @return 房间列表
     */
    private List<TbRoom> getRoomsByFloorId(Integer floorId) {
        return roomDataMapper.selectRoomsByFloorId(Long.valueOf(floorId));
    }
}